package com.docker.manege.server;

import com.alibaba.fastjson.JSONObject;
import com.docker.comment.ShellUtil;
import com.docker.core.mvc.Remark;
import com.docker.core.third.websocket.BaseWebsocketController;
import com.docker.core.third.websocket.WebsocketPool;
import com.jfinal.aop.Aop;

import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

/**
 * @Author zhl
 * @Create 2021/1/21 14:22
 **/
@Remark("")
@ServerEndpoint("/websocket/server/cmd")
public class DockerServerWebsocket extends BaseWebsocketController {

    private DockerServerService service = Aop.get(DockerServerService.class);

    @Override
    protected void onOpenBefore(Session session, String connectKey) {
    }

    @Override
    public void onOpenAfter(Session session, String connectKey) {
        String serverId = session.getRequestParameterMap().get("server_id").get(0);
        ShellUtil.connect("\r", session, serverId, 0, connectKey);
    }

    @Override
    public void onMessage(WebsocketPool socketPool, JSONObject msgJson) {
        try {
            service.onMessage(socketPool, msgJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCloseBefore(Session session, String connectKey) {
        ShellUtil.close(connectKey);
    }

    @Override
    protected void onCloseAfter(Session session, String connectKey) {
    }

    @Override
    protected void onErrorBefore(Session session, String connectKey) {
    }

    @Override
    protected void onErrorAfter(Session session, String connectKey) {
    }

}
